
//初始化
$(function(){
	
	//展示验证码
	LoginPage.showValidCode();
	
});

//逻辑代码块
var LoginPage = {
		
		/*获取表单信息*/
		getFormParams : function(){
			
			var account = $.trim($("#account").val());
			var password = $.trim($("#password").val());
			var errorValidCode = $.trim($("#verifyCode").val());
			
			var params =  {"account" : account , "password" : password , "errorValidCode" : errorValidCode};
			
			return params;
		},
		
		/*验证表单信息*/
		validateParams : function(params){
			
			if(params.account == ""){
				new $.zui.Messager('提示消息：用户不能为空!', {icon:'times',type: 'danger',placement: 'center',time:'2000'}).show();
				return false;
			}
			
			if(params.password == ""){
				new $.zui.Messager('提示消息：密码不能为空!', {icon:'times',type: 'danger',placement: 'center',time:'2000'}).show();
				return false;
			}
			
			return true;
		},
		
		/*登录*/
		login : function(){
			
			var params = this.getFormParams();
			
			if(!this.validateParams(params)) 
				return false;
			
			//密码md5加密
			params.password = hex_md5(params.password);
			
			var requestUrl = "/login/login";
			
			ajaxRequestByJSONParams(requestUrl , params , function(rst){
				
				if(rst.code == 200) {
					
					var token = rst.data;
			    	
					 window.localStorage.setItem("token" , token);
			    	
                    window.location.href = "/views/index.html";
                } else {
                	//每错一次 , 看一下是否需要加载验证码
                	LoginPage.showValidCode();  
                	new $.zui.Messager(rst.message, {icon:'times',type: 'danger',placement: 'center',time:'2000'}).show();
                }
				
			});
			
		},
		
		//显示验证码
		showValidCode : function(){
			
			var requestUrl = "/login/needShowLoginValidCode";
			
			ajaxRequestAsync(requestUrl , {} , function(rst){
				
				if(rst.code != 200) {
					new $.zui.Messager(rst.message, {icon:'times',type: 'danger',placement: 'center',time:'2000'}).show();
					return;
				}
				
				var flag = rst.data;
				
				//若需要展示 , 展示验证码
				if(flag){
					$("#verifyCodeLayer").show();
				}
				
			});
			
			
		}
		
}
